package org.easy.tool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.easy.tool.common.*;
import org.easy.tool.entity.User;
import org.easy.tool.mapper.UserMapper;
import org.easy.tool.service.IUser;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Valley
 * @since 2024-09-08 10:19:41
 */
@Service
public class UserService extends ServiceImpl<UserMapper, User> implements IUser {

    @Autowired
    UserMapper userMapper;

    public User getUserByLoginName(String loginName) {
        if (StringUtil.isBlank(loginName) || StringUtil.isBlank(loginName)) {
            return null;
        }
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getPhone, loginName).or().eq(User::getUsername, loginName);
        return getOne(wrapper);
    }

    public User register(String loginName, String password) {
        User user = new User();
        user.setUsername(loginName);
        user.setPassword(password);
        user.setNickname(loginName);
        user.setCreateTime(TimeUtil.nowMS());
        user.setUpdateTime(TimeUtil.nowMS());
        save(user);
        return user;
    }

    @Override
    public Object login(LinkedHashMap data) throws Exception {
        String loginName = (String) data.get("loginName");
        String loginPassword = (String) data.get("password");
        User userByLoginName = getUserByLoginName(loginName);
        if (userByLoginName == null) {
            throw new RuntimeException("账号/手机号未注册");
        }
        String password = userByLoginName.getPassword();
        String userId = userByLoginName.getId();
        if (!StringUtil.equals(password, loginPassword)) {
            throw new RuntimeException("密码错误");
        }
        return WebUtil.createToken(userId);
    }

    @Override
    public boolean isAdminUser(LinkedHashMap data) {
        User user = getUserByToken(data);
        return user.getAdminCode() != null;
    }

    public User getUserByToken(LinkedHashMap data) {
        String userId = getUserIdByToken(data);
        return getById(userId);
    }

    public String getUserIdByToken(LinkedHashMap data) {
        String token = (String) data.get("token");
        return WebUtil.parseToken(token);
    }

    @Override
    public boolean isLogin(LinkedHashMap data) throws Exception {
        User user = getUserByToken(data);
        return user != null;
    }

    @Override
    public Object ssoLogin(LinkedHashMap data) {
        String userStr = (String) data.get("user");
        User user = getUserByLoginName(userStr);
        return WebUtil.createToken(user.getId());
    }

    private User getUser(){
        String userId = WebUtil.getUserId();
        return getById(userId);
    }

    private String getUserDetail(){
        return getUser().getDetail();
    }

    @Override
    public Object getUserDetail(LinkedHashMap data) {
        String detail = getUserDetail();
        String type = (String) data.get("type");
        Object resData = null;
        if (StringUtil.isNotBlank(detail)){
            LinkedHashMap detailMap = (LinkedHashMap) JSONUtil.parse(detail, LinkedHashMap.class);
            resData = detailMap.get(type);
        }
        return resData;
    }

    @Override
    public Object updateUserDetail(LinkedHashMap data) {
        String detail = getUserDetail();
        String type = (String) data.get("type");
        Object content = data.get("content");
        LinkedHashMap detailMap = new LinkedHashMap();
        if (StringUtil.isBlank(detail)){
            detailMap.put(type, content);
        }else {
            detailMap = (LinkedHashMap) JSONUtil.parse(detail, LinkedHashMap.class);
            detailMap.put(type, content);
        }
        User user = getUser();
        user.setDetail(JSONUtil.toStr(detailMap));
        updateById(user);
        return true;
    }
}
